<?php
require_once('config.php');
// define variables and set to empty values
$firstNameErr = $lastNameErr = $usernameErr = $emailErr = $passwordErr = "";
$firstName = $lastName = $username = $email = $password = "";
		
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

//first name
	if (empty($_POST["firstName"]))
	{$firstNameErr = "First name is required";}
	else
	{
		$firstName = test_input($_POST["firstName"]);
		$_SESSION['firstName'] = $firstName;
		if (!preg_match("/^[a-zA-Z ]*$/",$firstName))
		{
			$firstNameErr = "Only letters please";
		}
	}
//last name
    if (empty($_POST["lastName"]))
    {
    $lastNameErr = "Last name is required";}
    else
    {
        $lastName = test_input($_POST["lastName"]);
        if (!preg_match("/^[a-zA-Z ]*$/",$lastName))
        {
            $lastNameErr = "Only letters please";
        }
    }
//email	
    if (empty($_POST["email"]))
    {
    $emailErr = "Email is required"; }
    else
    {
        $email = ($_POST["email"]);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $emailErr = "Please enter a valid email address";
        }
	}
//password
if (empty($_POST["password"]))
{
$passwordErr = "Password is required";}
else
{
$password = test_input($_POST["password"]);
}
	
// username	
if (empty($_POST["username"]))
{
$usernameErr = "Username is required";
}
  else
{
$username = test_input($_POST["username"]);
$_SESSION['username'] = $username;
 if (!preg_match("/^[a-zA-Z0-9]*$/",$username))
        {
            $usernameErr = "Only numbers and letters please";
        }
		}
		
}		
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}


if (empty($firstNameErr) && empty($lastNameErr) && empty($usernameErr) && empty($uniqueUserNameErr) && empty($emailErr) && empty($passwordErr) && (isset($_POST['studentSubmit']))) {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
	$query = "INSERT INTO student (firstName, lastName, username, email, password) VALUES ("
		. "'" . $firstName . "', "
		. "'" . $lastName . "', "
		. "'" . $username . "', "
		. "'" . $email . "', "
		. "'" . $password . "'"
		. ")"	;
$result = mysqli_query($con, $query);
header('LOCATION: test.php');
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/styles.css" type="text/css" rel="stylesheet"/>
<script src="scripts/scripts.js" type="text/javascript"></script>
<title>Register</title>
       <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>       
       <script type="text/javascript">
 
         $(document).ready(function(){
            $("#username").change(function(){
                 $("#message").html("<img src='img/ajax-loader.gif' /> checking...");       
 
            var username=$("#username").val();
 
              $.ajax({
                    type:"post",
                    url:"check.php",
                    data:"username="+username,
                        success:function(data){
                        if(data==0){
                            $("#message").html("<img src='img/tick.png' /> Username available");
                        }
                        else{
                            $("#message").html("<img src='img/cross.png' /> Username already taken");
                        }
                    }
                 });
 
            });
 
         });
 
       </script></head>
<body>
<div id="header">
</div>
<div id="container">
<div id="mainContent">
<h1 id="pageTitle">Assessment</h1>
<p><span class="error">* required field.</span></p>
 <form id="newStudentForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <label for="firstName" class="formLabel">First Name</label>
    <input type="text" name="firstName" id="firstName" value="<?php echo $firstName;?>"/>
       <span class="error">* <?php echo $firstNameErr;?></span>
<br/>
    <label for="lastName" class="formLabel">Last Name</label>
    <input type="text" name="lastName" id="lastName" value="<?php echo $lastName;?>"/>
      <span class="error">* <?php echo $lastNameErr;?></span>
 <br/>
    <label for="username" class="formLabel">User Name</label>
    <input type="text" name="username" id="username" value="<?php echo $username;?>" />
      <span class="error">* <?php echo $usernameErr;?></span>
<p id="message"></p>
</b> <br/>
    <label for="email" class="formLabel">Email</label>
    <input type="text" name="email" id="email" value="<?php echo $email;?>"/>
       <span class="error">* <?php echo $emailErr;?></span>
<br/>
    <label for="password" class="formLabel">Password</label>
    <input type="password" name="password" id="password" value="<?php echo $password;?>" maxlength="8" />
      <span class="error">* <?php echo $passwordErr;?></span>
    <input type="submit" id="studentSubmit" name="studentSubmit" value="Submit" class="btn" />
  </form>

</div>
</div>
</body>
</html>